package com.compiler.lexico.action;

import java.io.IOException;
import java.io.RandomAccessFile;
import com.compiler.lexico.token.SymbolTable;
import com.compiler.lexico.token.Token;
import com.compiler.logger.Logger;
import com.compiler.syntactic.Parser;

public class InsertConstUi extends AbstractAction {
	private final static String TYPE="Constante UInteger";
	private final static int MIN_VALUE=0;
	private final static int MAX_VALUE=65535;
	
	public InsertConstUi(Token token, SymbolTable symbolTable) {
		super(token,symbolTable);
	}

	private boolean checkRange() {
		int i = Integer.parseInt(this.token.getData());
		if(i >= MIN_VALUE && i <= MAX_VALUE)
			return true;
		return false;
	}
	
	@Override
	public void exec(RandomAccessFile raf) throws IOException {
		if(this.checkRange())
			this.symbolTable.addToken(new Token(token.getData(),TYPE,token.getLine(),Parser.IDENTIFICADOR));
		else Logger.getInstance().log("LEXICO", Logger.ERRORLevel, "Linea #"+this.token.getLine()+" ---> Constante fuera de rango ["+MIN_VALUE+"-"+MAX_VALUE+"]","LEXICO");
		this.token.clean();
	}

	@Override
	public String toString() {
		return "Insert";
	}

}
